package com.itheima.jobs;

import com.itheima.service.OrderSettingService;
import com.itheima.utils.DateUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.Date;

/**
 * 自定义Job,实现定时清理预约设置
 */
@RestController
public class ClearOrderSettingJob {
    @Reference
    private OrderSettingService orderSettingService;

    //根据日期删除一个月前的预约设置
    public void clearOrderSetting() {
        try {
            String date = DateUtils.parseDate2String(getFirstDay4ThisMonth2());
            orderSettingService.deleteByOrderDate(date);
            System.out.println("已清理" + date + "之前的预约设置");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //获得上上一个月的最后一天,(因为是每个月最后一天凌晨2点<也就是下一个月1日2时>执行清理任务)
    public static Date getFirstDay4ThisMonth2() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, 0);
        return calendar.getTime();
    }
}
